package com.yuan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Component
public class DynamicDataSourceConfig {
    @Bean
    @Primary
    public DynamicDataSource dataSource(@Qualifier("kylin1DataSource") DataSource dataSource1,
                                        @Qualifier("kylin2DataSource") DataSource dataSource2) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("master",dataSource1);
        targetDataSources.put("slave", dataSource2);
        return new DynamicDataSource(dataSource1, targetDataSources);
    }
}
